Appendix I — Assignment 4

Author

phonchi

Published

May 28, 2023

I.1 (1) What is a dictionary in Python?

  1. An ordered sequence of elements
  2. A collection of unique elements
  3. A collection of key-value pairs
  4. A sequence of key-value pairs

Ans: Double click to answer the question

I.2 (2) How do you access a value in a dictionary?

  1. By its index
  2. By its value
  3. By its key
  4. By its position

Ans: Double click to answer the question

I.3 (3) What is the syntax for adding an element to a dictionary in Python?

  1. my_dict.add(key, value)
  2. my_dict[key] = value
  3. my_dict.append(key, value)
  4. my_dict.extend(key, value)

Ans: Double click to answer the question

I.4 (4) What is a set in Python?

  1. An ordered sequence of elements
  2. A collection of unique elements
  3. A collection of key-value pairs
  4. A sequence of key-value pairs

Ans: Double click to answer the question

I.5 (5) What is the syntax for creating a set in Python?

  1. my_set = {1, 2, 3}
  2. my_set = [1, 2, 3]
  3. my_set = (1, 2, 3)
  4. my_set = {‘1’, ‘2’, ‘3’}

Ans: Double click to answer the question

I.6 (6) What is the difference between a dictionary and a list in Python, and when would you choose to use one over the other?

Ans: Double click to answer the question

The main difference between a dictionary and a list in Python is that a dictionary stores data as key-value pairs, while a list stores data as a sequence of elements. You would choose to use a dictionary over a list when you need to store data in a way that can be easily looked up based on a unique key.

I.7 (7) How do you check if a key exists in a dictionary in Python?

Ans: Double click to answer the question

You can use the in operator to check if a key exists in a dictionary. For example: if key in my_dict:

I.8 (8) How do you create a deep copy of a dictionary in Python?

Ans: Double click to answer the question

You can create a deep copy of a dictionary in Python using the copy() method with the .deepcopy() method from the copy module.
For example:
import copy
new_dict = copy.deepcopy(my_dict)

I.9 (9) Suppose you have a list of student grades, and you want to find the top-performing student and the average grade for each subject. Write a program that does the following:

  1. Create a dictionary called grades that maps subject names to lists of grades for each student. Assume that each student has the same number of grades for each subject.
  2. Create a dictionary called subject_averages that maps subject names to their average grades across all students.
  3. Create a dictionary called student_totals that maps student names to their total grades across all subjects.
  4. Create a variable called top_student that contains the name of the student with the highest total grade.
  5. Print out the top-performing student’s name and total grade, as well as the average grade for each subject.
grades = {
    'Math': [[90, 85, 92, 87, 94], [95, 88, 91, 89, 92], [87, 83, 85, 90, 89]],
    'Science': [[82, 88, 89, 91, 85], [90, 86, 88, 87, 92], [85, 83, 87, 84, 89]],
    'English': [[88, 85, 92, 86, 90], [91, 87, 89, 92, 85], [83, 86, 88, 87, 90]],
}

# coding your answer here

# Create dictionary to store subject averages
subject_averages = {}

# Create dictionary to store student totals
student_totals = {}

# Calculate subject averages and student totals
for subject, subject_grades in grades.items():
    subject_total = 0
    for student_grades in subject_grades:
        student_name = f"Student {subject_grades.index(student_grades) + 1}"
        student_total = sum(student_grades)
        student_totals[student_name] = student_totals.get(student_name, 0) + student_total
        subject_total += student_total
    subject_averages[subject] = subject_total / (len(subject_grades) * len(subject_grades[0]))

# Find top-performing student
top_student = max(student_totals, key=student_totals.get)

# Print results
print(f"Top-performing student: {top_student}, Total grade: {student_totals[top_student]}")
for subject, average in subject_averages.items():
    print(f"{subject} average grade: {average}")
Top-performing student: Student 2, Total grade: 1342
Math average grade: 89.13333333333334
Science average grade: 87.06666666666666
English average grade: 87.93333333333334

I.10 (10) Write a program that creates a dictionary students that maps student names to their ages. The program should prompt the user to enter the names and ages of at least three students, and then print out the contents of the students dictionary.

sample output:

Enter a student name (or 'q' to quit): Sherry   
Enter the student's age: 18   
Enter a student name (or 'q' to quit): Money   
Enter the student's age: 3   
Enter a student name (or 'q' to quit): Steve   
Enter the student's age: 30   
Enter a student name (or 'q' to quit): q   
Student dictionary:   
{'Sherry': '18', 'Money': '3', 'Steve': '30'}
# coding your answer here
students = {}

while True:
    name = input("Enter a student name (or 'q' to quit): ")
    if name == 'q':
        break
    age = input("Enter the student's age: ")
    students[name] = age

print("Student dictionary:")
print(students)